package com.cg.design.graphicdesignpattern.visitor;

import org.junit.Test;

import java.util.Iterator;

/**
 * Visitor访问者模式测试类
 *
 * @program: LeetCode->VisitorTest
 * @description: Visitor访问者模式测试类
 * @author: cg
 * @create: 2022-03-26 21:12
 **/
public class VisitorTest {

    @Test
    public void testVisitor() {
        try {
            System.out.println("Making root entries...");
            Directory rootDir = new Directory("root");
            Directory binDir = new Directory("bin");
            Directory tmpDir = new Directory("tmp");
            Directory userDir = new Directory("user");
            rootDir.add(binDir);
            rootDir.add(tmpDir);
            rootDir.add(userDir);
            binDir.add(new File("vi", 10000));
            binDir.add(new File("latex", 20000));
            rootDir.accept(new ListVisitor());

            System.out.println("");
            System.out.println("Making user entries...");
            Directory yuki = new Directory("yuki");
            Directory hanako = new Directory("hanako");
            Directory tomura = new Directory("tomura");
            userDir.add(yuki);
            userDir.add(hanako);
            userDir.add(tmpDir);
            yuki.add(new File("diary.html", 100));
            yuki.add(new File("Composite.java", 200));
            hanako.add(new File("memo.tex", 300));
            hanako.add(new File("index.html", 350));
            tomura.add(new File("game.doc", 400));
            tomura.add(new File("junk.mail", 500));

            FileFindVisitor findVisitor = new FileFindVisitor(".html");
            rootDir.accept(findVisitor);
            System.out.println("HTML files are:");
            Iterator<Entry> files = findVisitor.getFoundFiles();
            while (files.hasNext()) {
                Entry entry = files.next();
                System.out.println(entry.toString());
            }

            System.out.println("");
            Directory root1 = new Directory("root1");
            root1.add(new File("diary.html", 10));
            root1.add(new File("index.html", 20));
            ElementArrayList list = new ElementArrayList();
            list.add(root1);
            list.add(rootDir);
            list.add(new File("etc.html", 1234));
            list.accept(new ListVisitor());
        } catch (FileTreatmentException e) {
            e.printStackTrace();
        }
    }

}
